﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NetMqClient.Model;
using NetMQ;
using NetMQ.Sockets;
using System.Threading;

namespace NetMqClient
{
    internal class Program
    {
        private static void Main()
        {
            Console.WriteLine("Creating socket..");

            Thread pushthread = new Thread(() =>
            {
                using (var pushSocket = new PushSocket(">tcp://localhost:5556")
                {
                    /* So apparently this is needed to prevent the socket from being immediately disposed
                       after the last instruction of the using block.
                       With this setting, the socket shall be disposed only after the message has been sent.
                       This is equivalent to say "wait forever", but someone may arguably want to set a finite 
                       amount of time when in production.
                    */
                    Options = { Linger = TimeSpan.FromSeconds(-1) }
                })
                {
                    int messageid = 1;
                    while (true)
                    {
                        
                        Console.WriteLine("Creating message..");
                        var message = new Message
                        {
                            Text = $"SendMessageID Is{messageid}",
                            TimeStamp = DateTime.Now,
                            Type = "sendmessage",
                            ValueCode = 1
                        };
                        //Console.WriteLine("Serializing message..");
                        using (var memoryStream = new MemoryStream())
                        {
                            var binaryFormatter = new BinaryFormatter();
                            binaryFormatter.Serialize(memoryStream, message);
                            var serializedMessage = memoryStream.ToArray();
                            Console.WriteLine("Sending message..");
                            pushSocket.SendFrame(serializedMessage);
                        }
                        messageid++;
                        Thread.Sleep(500);
                    }

                }
            });
            Thread receivethread = new Thread(() =>
            {
                using (var pullSocket = new PullSocket("@tcp://localhost:5557"))
                {

                    Console.WriteLine("Receiving message..\n\n");
                    while (true)
                    {
                        var serializedMessage = pullSocket.ReceiveFrameBytes();
                        using (var memoryStream = new MemoryStream(serializedMessage))
                        {
                            var binaryFormatter = new BinaryFormatter();
                            var message = binaryFormatter.Deserialize(memoryStream) as Message;
                            Console.WriteLine($"Message contents: \n" +
                                                $"\tText: {message?.Text}\n" +
                                                $"\tType: {message?.Type}\n" +
                                                $"\tTimestamp: {message?.TimeStamp}\n" +
                                                $"\tValueCode: {message?.ValueCode}\n");
                            Thread.Sleep(500);
                        }
                    }
                }
            });
            pushthread.Start();
            receivethread.Start();
            Console.WriteLine("WaitingKey");
            Console.ReadKey();
        }
    }
}
                
      
 
